
#include "../mainctrl.h"
#include "commmsg.h"
#include "CCmdAuctionPayBack.h"
#include "../act/CAuctionMgr.h"
#include "../cmd/dataprepare.h"

CT_REGISTER_ASYNCCMD(ID_MAIN_INNER_AUCTION_PAYBACK , CCmdAuctionPayBack);

int CCmdAuctionPayBack::Do(void* pData)
{    
    LogicDo(RET_OK);
 
    return RET_OK;
}

int CCmdAuctionPayBack::LogicDo(int iRet)
{
    return CallBack(RET_OK);
}

int CCmdAuctionPayBack::CallBack (int iRet)
{
    CAuctionBlindData & stBlindData = pstMainCtrl->m_stAuctionMgr._stBlindData;

    unsigned int uiGid = 0;
    if (iRet != RET_OK)
    {
        uiGid = stBlindData._astNeedBackMoneyGid[0];
        CT_WARNING(("CCmdAuction ret %u, %d", iRet, uiGid));
        stBlindData._astNeedBackMoneyGid.DelOneItem(0);
    }

    if (stBlindData._astNeedBackMoneyGid.Size() == 0)
    {
        return Done(RET_OK);
    }

    uiGid = stBlindData._astNeedBackMoneyGid[0];

    CAuctionRecord & stBlind = pstMainCtrl->m_stZoneOnlineData._stZoneData._stAuctionZoneData._stTodayHis._stBlind;
    if ((uiGid != stBlind._uiSilverGID) && (uiGid != stBlind._uiGoldGID))
    {
        //在线用户和暗拍物品得住实时退款，非在线用户登录时退款
        if (!pstMainCtrl->m_stPlayerOnline.CheckOnline(uiGid))
        {
            stBlindData._astNeedBackMoneyGid.DelOneItem(0);
            return CallBack(RET_OK);
        }
    }

    PREPARE_PLAYER_BASE_INFO(this, uiGid);
    int iIndex = pstMainCtrl->m_stPlayerBaseInfoHash.HashFind(uiGid);
    if (iIndex < 0)
    {
        CT_WARNING(("%u", uiGid));
        return CallBack(RET_SYS_ERROR);
    }
    CPlayerBaseInfo & stPlayerBaseInfo = pstMainCtrl->m_stPlayerBaseInfoHash[iIndex];
    
    if (stBlind._iGold > 0 || stBlind._iSilver > 0)
    {
        iRet = pstMainCtrl->m_stAuctionMgr.PayBackOneUserBlindMoney(stPlayerBaseInfo, stBlind);
        if (RET_OK == iRet)
        {
            CVarObjectKey stKey;
            stKey.m_chDataType = DT_PLAYER_BASE_INFO;
            stKey.m_u64ID = uiGid;
            pstMainCtrl->UpdateData(stKey, stPlayerBaseInfo, VOP_UPDATE);

            stBlindData._astNeedBackMoneyGid.DelOneItem(0);

            int iIndex = pstMainCtrl->m_stPlayerOnlineDataHash.HashFind(uiGid);
            if (iIndex >= 0)
            {
                pstMainCtrl->m_stPlayerOnlineDataHash[iIndex]._stUserAuction = stPlayerBaseInfo._stUserAuctionData;
            }
        }
        else
        {
            CT_ERROR(("%d, %d", iRet, uiGid));
        }
    }

    PushQueue(RET_OK);
    return 0;
}
